package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.adapter.MySendProcurementAdapter;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.net.responses.SendProcurementResponse;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.NetUtil;
import com.redwine.app.view.EmptyView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.redwine.app.utils.Utils.showToast;

/**
 * 我发布的采购
 * 作者：gassion on 17/9/9 21:59
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class MySendProcurementActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.my_send_procurement_tv_all)
    private TextView mMySendProcurementTvAll;
    @ViewInject(R.id.my_send_procurement_ll_all)
    private LinearLayout mMySendProcurementLlAll;
    @ViewInject(R.id.my_send_procurement_tv_shz)
    private TextView mMySendProcurementTvShz;
    @ViewInject(R.id.my_send_procurement_ll_shz)
    private LinearLayout mMySendProcurementLlShz;
    @ViewInject(R.id.my_send_procurement_tv_qgz)
    private TextView mMySendProcurementTvQgz;
    @ViewInject(R.id.my_send_procurement_ll_qgz)
    private LinearLayout mMySendProcurementLlQgz;
    @ViewInject(R.id.my_send_procurement_tv_jygb)
    private TextView mMySendProcurementTvJygb;
    @ViewInject(R.id.my_send_procurement_ll_jygb)
    private LinearLayout mMySendProcurementLlJygb;
    @ViewInject(R.id.my_send_procurement_refresh_list_view)
    private PullToRefreshListView mListView;
    @ViewInject(R.id.empty_view)
    private EmptyView emptyView;
    private int mSetStatus = 0;
    private int page = 1;//默认加载第一页
    private MySendProcurementAdapter mySendProcurementAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_send_procurement);
        ViewUtils.inject(this);
        EventBus.getDefault().register(this);
        initView();
        requestListData(true);
    }

    private void initView() {
        headerTitle.setText(R.string.me_send_procurement);
        layoutBack.setVisibility(View.VISIBLE);
        mSetStatus = 0;
        mMySendProcurementLlAll.setBackgroundResource(R.drawable.supply_left_shape_select);
        mMySendProcurementTvAll.setTextColor(getResources().getColor(R.color.white));
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                requestListData(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page += 1;
                requestListData(false);
            }
        });
        mListView.setOnActionMoveListener(new PullToRefreshBase.OnActionMove() {
            @Override
            public void onMove(boolean isMove) {
                setUpdateTime(mListView);
            }
        });
        mySendProcurementAdapter = new MySendProcurementAdapter(this);
        mListView.setAdapter(mySendProcurementAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SendProcurementResponse.DataEntityX.DataEntity dataEntity = (SendProcurementResponse.DataEntityX.DataEntity) parent.getItemAtPosition(position);
                Intent intent = new Intent(MySendProcurementActivity.this, ProcurementDetailActivity.class);
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_NAME, dataEntity.getTitle());
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_UID, dataEntity.getUid());
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_ID, dataEntity.getId());
                startActivity(intent);
            }
        });
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mListView.setEmptyView(emptyView);
        emptyView.bind();
    }

    @OnClick({R.id.layout_back, R.id.my_send_procurement_ll_all, R.id.my_send_procurement_ll_shz,
            R.id.my_send_procurement_ll_qgz, R.id.my_send_procurement_ll_jygb})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.my_send_procurement_ll_all:
                //全部
                mSetStatus = 0;
                requestListData(true);
                break;

            case R.id.my_send_procurement_ll_shz:
                //审核中
                mSetStatus = 3;
                requestListData(true);
                break;

            case R.id.my_send_procurement_ll_qgz:
                //求购中
                mSetStatus = 1;
                requestListData(true);
                break;

            case R.id.my_send_procurement_ll_jygb:
                //交易关闭
                mSetStatus = 2;
                requestListData(true);
                break;

        }
    }


    /**
     * 设置刷新状态
     * status  状态 传入0 全部  1 采购中  2交易关闭  3审核中
     */
    private void setFlushStatus() {

        switch (mSetStatus) {

            case 0:
                mMySendProcurementLlAll.setBackgroundResource(R.drawable.supply_left_shape_select);
                mMySendProcurementTvAll.setTextColor(getResources().getColor(R.color.white));
                mMySendProcurementTvQgz.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlQgz.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvShz.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlShz.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvJygb.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlJygb.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                break;

            case 1:
                mMySendProcurementLlQgz.setBackgroundResource(R.drawable.supply_middle_shape_select);
                mMySendProcurementTvQgz.setTextColor(getResources().getColor(R.color.white));
                mMySendProcurementTvAll.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlAll.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvShz.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlShz.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvJygb.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlJygb.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                break;

            case 3:
                mMySendProcurementLlShz.setBackgroundResource(R.drawable.supply_middle_shape_select);
                mMySendProcurementTvShz.setTextColor(getResources().getColor(R.color.white));
                mMySendProcurementTvJygb.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlJygb.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvQgz.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlQgz.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvAll.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlAll.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                break;

            case 2:
                mMySendProcurementLlJygb.setBackgroundResource(R.drawable.supply_right_shape_select);
                mMySendProcurementTvJygb.setTextColor(getResources().getColor(R.color.white));
                mMySendProcurementTvShz.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlShz.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvQgz.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlQgz.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                mMySendProcurementTvAll.setTextColor(getResources().getColor(R.color.dark_gray_3));
                mMySendProcurementLlAll.setBackgroundColor(getResources().getColor(R.color.tranparnt));
                break;

        }

    }

    /**
     * 我发布的采购
     */
    private void requestListData(boolean isShowWaiting) {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            setErrorView(emptyView, mListView);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (null != mListView)
                        mListView.onRefreshComplete();

                }
            }, 500);
            return;
        }

        if (isShowWaiting)
            showWaiting();
        String url = NetworkConstants.MY_PURCHASE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        if (mSetStatus != 0)
            params.put("status", String.valueOf(mSetStatus));
        params.put("size", "20");
        params.put("uid", Global.user.getUid());
        params.put("page", String.valueOf(page));
        GsonRequest<SendProcurementResponse> request = new GsonRequest<>(url, Request.Method.POST,
                SendProcurementResponse.class, false, params, new Response.Listener<SendProcurementResponse>() {
            @Override
            public void onResponse(SendProcurementResponse response) {
                dismissWaiting();
                if (null != mListView)
                    mListView.onRefreshComplete();
                if (response.isSuccess()) {
                    setFlushStatus();
                    SendProcurementResponse.DataEntityX responseData = response.getData();
                    List<SendProcurementResponse.DataEntityX.DataEntity> dataEntities = responseData.getData();
                    if (null != dataEntities && !dataEntities.isEmpty()) {
                        mySendProcurementAdapter.setData(page, dataEntities);
                    } else {
                        setEmptyView(emptyView, mListView);
                        mySendProcurementAdapter.setData(page, dataEntities);
                    }
                    if (responseData.getPage() != responseData.getAllpage()) {
                        mListView.setMode(PullToRefreshBase.Mode.BOTH);
                    } else {
                        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    }
                } else {
                    setEmptyView(emptyView, mListView);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
                if (null != mListView)
                    mListView.onRefreshComplete();
                showToast(R.string.generic_check);
                setErrorView(emptyView, mListView);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }

    public void onEventMainThread(SendMsgEvent event) {
        if (event.getSendType() == SendMsgEvent.REFURBISH_MAIN_DATA) {
            requestListData(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void emptyOnclick() {
        emptyView.bind();
        if (null != mListView) {
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    mListView.setRefreshing();
                }
            }, 500);
        } else {
            requestListData(true);
        }
    }


}
